package com.lantosec.hmrp.modules.report.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.report.entity.*;
import com.lantosec.hmrp.modules.report.mapper.BusinessAnalysisMapper;
import com.lantosec.hmrp.modules.report.service.IBusinessAnalysisService;
import com.lantosec.hmrp.modules.report.vo.JyfxAchievement;
import com.lantosec.hmrp.modules.report.vo.JyfxIncome;
import com.lantosec.hmrp.modules.report.vo.JyfxIndexAnalysis;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Primary
@Log4j2
public class BusinessAnalysisServiceImpl extends ServiceImpl<BusinessAnalysisMapper, ImpHsptJyfx> implements IBusinessAnalysisService {

    @Override
    public Map<String, Object> getHsptJyfxChartsByMonthCd(String monthCd) {
        Map<String, Object> data = new HashMap<String, Object>();
        // 经营分析全院数据 一个月一条数据
        ImpHsptJyfx newImpHsptJyfx = baseMapper.getHsptJyfxByMonthCd(monthCd);
        //封装 收入占比图数据 收入柱状图数据 门诊饼图 住院饼图
        Map<String, Object> encapsulationIncomeStructureData = encapsulationIncomeStructureData(newImpHsptJyfx);
        data.put("incomeStructureChartDataByMonthCd", encapsulationIncomeStructureData);

        //计算出前一年的当前月份
        int parseInt = Integer.parseInt(monthCd.substring(0, 4));
        String tqMonthCd = String.valueOf(parseInt - 1) +"-"+ monthCd.substring(5, 7);

        //查询出去年的同期数据
        ImpHsptJyfx oldImpHsptJyfx = baseMapper.getHsptJyfxByMonthCd(tqMonthCd);

        //封装 绩效发放图表 数据 一个柱状图
        List<JyfxAchievement> encapsulationAchievementData = encapsulationAchievementData(newImpHsptJyfx, oldImpHsptJyfx);
        data.put("achievementChartDataByMonthCd", encapsulationAchievementData);

        List<JyfxIndexAnalysis> encapsulationIndexAnalysis = encapsulationIndexAnalysis(newImpHsptJyfx, oldImpHsptJyfx);
        data.put("indexAnalysisChartDataByMonthCd", encapsulationIndexAnalysis);

        return data;
    }

    @Override
    public List<Map<String, Object>> getJyfxZyksGridByMonthCd(String month) {
        //计算出前一年的当前月份
        int parseInt = Integer.parseInt(month.substring(0, 4));
        String oldMonth = String.valueOf(parseInt - 1) +"-"+ month.substring(5, 7);
        List<Map<String, Object>> list =  baseMapper.getJyfxZyksGridByMonthCd(month,oldMonth);
        return list;
    }







    /**
     * 封装 收入占比图数据 收入柱状图数据 门诊饼图 住院饼图
     */
    private Map<String, Object> encapsulationIncomeStructureData(ImpHsptJyfx newImpHsptJyfx){
        // 封装 收入结构
        Map<String, Object> incomeStructureData = new HashMap<String, Object>();
        if(null != newImpHsptJyfx){
            // 封装收入占比图数据
            Map<String, Object> incomeRatioData = new HashMap<String, Object>();
            if(null != newImpHsptJyfx.getTotalInc() && !newImpHsptJyfx.getTotalInc().equals("0")){
                incomeRatioData.put("totalInc",newImpHsptJyfx.getTotalInc()); //总收入
                incomeRatioData.put("clinicInc",newImpHsptJyfx.getClinicInc()); //门诊总收入
                incomeRatioData.put("innerInc",newImpHsptJyfx.getInnerInc());//住院总收入
                incomeRatioData.put("mincForShowzb",newImpHsptJyfx.getMincForShowzb());//门诊收入占比
                incomeRatioData.put("zysrzb",newImpHsptJyfx.getZysrzb());//住院收入占比
                incomeStructureData.put("incomeRatioData",incomeRatioData);
            }else {
                incomeStructureData.put("incomeRatioData",incomeRatioData);
            }
            // 封装收入柱状图数据
            List<JyfxIncome> incomeBardata = encapsulationIncomeBardata(newImpHsptJyfx);
            incomeStructureData.put("incomeBardata", incomeBardata);

            //封装门诊饼图
            Map<String, Object> encapsulationJyfxMzIncomePieData = encapsulationJyfxMzIncomePieData(newImpHsptJyfx);
            incomeStructureData.put("mzIncomePieData", encapsulationJyfxMzIncomePieData);

            //封装住院饼图
            Map<String, Object> encapsulationJyfxZyIncomePieData = encapsulationJyfxZyIncomePieData(newImpHsptJyfx);
            incomeStructureData.put("zyIncomePieData", encapsulationJyfxZyIncomePieData);
        }
        return  incomeStructureData;
    }

    /**
     * 封装收入柱状图数据
     * @param newImpHsptJyfx
     * @return
     */
    private List<JyfxIncome> encapsulationIncomeBardata(ImpHsptJyfx newImpHsptJyfx){
        List<JyfxIncome> data = new ArrayList<JyfxIncome>();
        JyfxIncome ypIncome = new JyfxIncome("药费", newImpHsptJyfx.getClinicDrugInc(),newImpHsptJyfx.getInnerDrugInc(), "万元");
        data.add(ypIncome);
        JyfxIncome jcIncome = new JyfxIncome("医技检查费", newImpHsptJyfx.getClinicChkInc(),newImpHsptJyfx.getInnerChkInc(), "万元");
        data.add(jcIncome);
        JyfxIncome zlIncome = new JyfxIncome("治疗费", newImpHsptJyfx.getClinicTreatInc(),newImpHsptJyfx.getInnerTreatInc(), "万元");
        data.add(zlIncome);
        JyfxIncome ssIncome = new JyfxIncome("手术费", newImpHsptJyfx.getClinicOperInc(),newImpHsptJyfx.getInnerOperInc(), "万元");
        data.add(ssIncome);
        JyfxIncome clIncome = new JyfxIncome("材料费", newImpHsptJyfx.getClinicMaterialInc(),newImpHsptJyfx.getInnerMaterialInc(), "万元");
        data.add(clIncome);
        BigDecimal b = new BigDecimal(0);
        JyfxIncome cwIncome = new JyfxIncome("床位费", b, newImpHsptJyfx.getInnerBedInc(), "万元");
        data.add(cwIncome);
        JyfxIncome qtIncome = new JyfxIncome("其他", newImpHsptJyfx.getClinicOtherInc(),newImpHsptJyfx.getInnerOtherInc(), "万元");
        data.add(qtIncome);
        return data;
    }

    /**
     * 封装门诊饼图
     * @param newImpHsptJyfx
     * @return
     */
    private Map<String, Object> encapsulationJyfxMzIncomePieData(ImpHsptJyfx newImpHsptJyfx){

        Map<String, Object> data=new HashMap<String, Object>();
        List<JyfxIncome> jyfxIncomeList=new ArrayList<JyfxIncome>();
        BigDecimal b = new BigDecimal(0);
        JyfxIncome ypIncome = new JyfxIncome("药费", newImpHsptJyfx.getClinicDrugInc(), b, "万元");
        jyfxIncomeList.add(ypIncome);

        JyfxIncome jcIncome = new JyfxIncome("医技检查费", newImpHsptJyfx.getClinicChkInc(), b, "万元");
        jyfxIncomeList.add(jcIncome);

        JyfxIncome zlIncome = new JyfxIncome("治疗费", newImpHsptJyfx.getClinicTreatInc(), b, "万元");
        jyfxIncomeList.add(zlIncome);

        JyfxIncome ssIncome = new JyfxIncome("手术费", newImpHsptJyfx.getClinicOperInc(), b, "万元");
        jyfxIncomeList.add(ssIncome);

        JyfxIncome clIncome = new JyfxIncome("材料费", newImpHsptJyfx.getClinicMaterialInc(), b, "万元");
        jyfxIncomeList.add(clIncome);

        JyfxIncome qtIncome = new JyfxIncome("其他", newImpHsptJyfx.getClinicOtherInc(), b, "万元");
        jyfxIncomeList.add(qtIncome);
        data.put("jyfxIncomeList", jyfxIncomeList);
        data.put("mzzsrData", newImpHsptJyfx.getClinicInc());
        return data;
    }

    /**
     * 封装住院饼图
     * @param newImpHsptJyfx
     * @return
     */
    private Map<String, Object> encapsulationJyfxZyIncomePieData(ImpHsptJyfx newImpHsptJyfx){

        Map<String, Object> data=new HashMap<String, Object>();
        List<JyfxIncome> jyfxIncomeList=new ArrayList<JyfxIncome>();
        BigDecimal b = new BigDecimal(0);
        JyfxIncome ypIncome = new JyfxIncome("药费", b, newImpHsptJyfx.getInnerDrugInc(), "万元");
        jyfxIncomeList.add(ypIncome);

        JyfxIncome jcIncome = new JyfxIncome("医技检查费", b, newImpHsptJyfx.getInnerChkInc(), "万元");
        jyfxIncomeList.add(jcIncome);

        JyfxIncome zlIncome = new JyfxIncome("治疗费", b, newImpHsptJyfx.getInnerTreatInc(), "万元");
        jyfxIncomeList.add(zlIncome);

        JyfxIncome ssIncome = new JyfxIncome("手术费", b, newImpHsptJyfx.getInnerOperInc(), "万元");
        jyfxIncomeList.add(ssIncome);

        JyfxIncome clIncome = new JyfxIncome("材料费", b, newImpHsptJyfx.getInnerMaterialInc(), "万元");
        jyfxIncomeList.add(clIncome);

        JyfxIncome qtIncome = new JyfxIncome("其他", b, newImpHsptJyfx.getInnerOtherInc(), "万元");
        jyfxIncomeList.add(qtIncome);

        JyfxIncome cwIncome = new JyfxIncome("床位费", b, newImpHsptJyfx.getInnerBedInc(), "万元");
        jyfxIncomeList.add(cwIncome);
        data.put("jyfxIncomeList", jyfxIncomeList);
        data.put("zyzsrData", newImpHsptJyfx.getInnerInc());
        return data;
    }

    /**
     * 封装 绩效发放图表 数据 一个柱状图
     * @param newImpHsptJyfx
     * @param oldImpHsptJyfx
     * @return
     */
    private List<JyfxAchievement> encapsulationAchievementData(ImpHsptJyfx newImpHsptJyfx, ImpHsptJyfx oldImpHsptJyfx) {

        List<JyfxAchievement> achievementData = new ArrayList<JyfxAchievement>();
        if (null != newImpHsptJyfx) {
            JyfxAchievement achievement = new JyfxAchievement("万元", newImpHsptJyfx.getPerf(), newImpHsptJyfx.getTotalInc(), "本期绩效");
            achievementData.add(achievement);
            if (null != oldImpHsptJyfx) {
                JyfxAchievement tqAchievement = new JyfxAchievement("万元", oldImpHsptJyfx.getPerf(), oldImpHsptJyfx.getTotalInc(), "同期绩效");
                achievementData.add(tqAchievement);
            } else {
                JyfxAchievement tqAchievement = new JyfxAchievement();
                achievementData.add(tqAchievement);
            }
        }
        return achievementData;
    }

    public List<JyfxIndexAnalysis> encapsulationIndexAnalysis(ImpHsptJyfx newImpHsptJyfx, ImpHsptJyfx oldImpHsptJyfx) {

        List<JyfxIndexAnalysis> indexAnalysisData = new ArrayList<JyfxIndexAnalysis>();
        //计算出总收入的增长率
        BigDecimal rateTotal = getRate(newImpHsptJyfx.getTotalInc(), oldImpHsptJyfx.getTotalInc());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getTotalInc(),oldImpHsptJyfx.getTotalInc(),rateTotal,"总收入"));

        //计算出门诊收入的增长率
        BigDecimal rateClinicInc = getRate(newImpHsptJyfx.getClinicInc(), oldImpHsptJyfx.getClinicInc());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getClinicInc(),oldImpHsptJyfx.getClinicInc(),rateClinicInc,"门诊收入"));

        //计算门诊均次费用的增长率
        BigDecimal rateClinicAvgFee = getRate(newImpHsptJyfx.getClinicAvgFee(), oldImpHsptJyfx.getClinicAvgFee());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getClinicAvgFee(),oldImpHsptJyfx.getClinicAvgFee(),rateClinicAvgFee,"门诊均次费用"));

        //计算出住院收入的增长率
        BigDecimal rateInnerInc = getRate(newImpHsptJyfx.getInnerInc(), oldImpHsptJyfx.getInnerInc());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getInnerInc(),oldImpHsptJyfx.getInnerInc(),rateInnerInc,"住院收入"));

        //计算住院均次费用的增长率
        BigDecimal rateInnerAvgFee = getRate(newImpHsptJyfx.getInnerAvgFee(), oldImpHsptJyfx.getInnerAvgFee());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getInnerAvgFee(),oldImpHsptJyfx.getInnerAvgFee(),rateInnerAvgFee,"住院均次费用"));

        //计算出绩效发放的增长率
        BigDecimal ratePerf = getRate(newImpHsptJyfx.getPerf(), oldImpHsptJyfx.getPerf());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getPerf(),oldImpHsptJyfx.getPerf(),ratePerf,"绩效发放"));

        //计算出门诊总人次的增长率
        BigDecimal rateClinicEtNum = getRate(newImpHsptJyfx.getClinicEtNum(), oldImpHsptJyfx.getClinicEtNum());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getClinicEtNum(),oldImpHsptJyfx.getClinicEtNum(),rateClinicEtNum,"门诊总人次"));

        //计算出门诊人次的增长率
        BigDecimal rateClinicNum = getRate(newImpHsptJyfx.getClinicNum(), oldImpHsptJyfx.getClinicNum());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getClinicNum(),oldImpHsptJyfx.getClinicNum(),rateClinicNum,"门诊人次"));

        //计算出急诊人次的增长率
        BigDecimal rateEtNum = getRate(newImpHsptJyfx.getEtNum(), oldImpHsptJyfx.getEtNum());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getEtNum(),oldImpHsptJyfx.getEtNum(),rateEtNum,"急诊人次"));

        //计算出入院人次的增长率
        BigDecimal rateInNum = getRate(newImpHsptJyfx.getInNum(), oldImpHsptJyfx.getInNum());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getInNum(),oldImpHsptJyfx.getInNum(),rateInNum,"入院人次"));

        //计算出出院人数的增长率
        BigDecimal rateOutNum = getRate(newImpHsptJyfx.getOutNum(), oldImpHsptJyfx.getOutNum());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getOutNum(),oldImpHsptJyfx.getOutNum(),rateOutNum,"出院人次"));

        //计算出在院人次的增长率
        BigDecimal rateUsedBeds = getRate(newImpHsptJyfx.getUsedBeds(), oldImpHsptJyfx.getUsedBeds());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getUsedBeds(),oldImpHsptJyfx.getUsedBeds(),rateUsedBeds,"在院人次"));

        //计算出分娩人次的增长率
        BigDecimal rateBornNum = getRate(newImpHsptJyfx.getBornNum(), oldImpHsptJyfx.getBornNum());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getBornNum(),oldImpHsptJyfx.getBornNum(),rateBornNum,"分娩人次"));

        //计算出手术人次的增长率
        BigDecimal rateOperNum = getRate(newImpHsptJyfx.getOperNum(), oldImpHsptJyfx.getOperNum());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getOperNum(),oldImpHsptJyfx.getOperNum(),rateOperNum,"手术人次"));

        //计算出额定床位的增长率
        BigDecimal rateApproveBeds = getRate(newImpHsptJyfx.getApproveBeds(), oldImpHsptJyfx.getApproveBeds());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getApproveBeds(),oldImpHsptJyfx.getApproveBeds(),rateApproveBeds,"额定床位"));

        //计算出使用床位的增长率
        BigDecimal rateInnerUseBeds = getRate(newImpHsptJyfx.getInnerUseBeds(), oldImpHsptJyfx.getInnerUseBeds());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getInnerUseBeds(),oldImpHsptJyfx.getInnerUseBeds(),rateInnerUseBeds,"使用床位"));

        //计算出床位使用率的增长率
        BigDecimal rateBedUseRate = getRate(newImpHsptJyfx.getBedUseRate(), oldImpHsptJyfx.getBedUseRate());
        indexAnalysisData.add(new JyfxIndexAnalysis("万元",newImpHsptJyfx.getBedUseRate(),oldImpHsptJyfx.getBedUseRate(),rateBedUseRate,"床位使用率"));

        return indexAnalysisData;
    }

    /**
     * 计算增长率
     * @param newValue
     * @param oldValue
     * @return
     */
    private BigDecimal getRate(BigDecimal newValue, BigDecimal oldValue) {
        BigDecimal rate = new BigDecimal(0) ;
        if(newValue.compareTo(BigDecimal.ZERO) == 0 && oldValue.compareTo(BigDecimal.ZERO) != 0){
            rate = oldValue.multiply(new BigDecimal(-1));
        }
        if(newValue.compareTo(BigDecimal.ZERO) != 0 && oldValue.compareTo(BigDecimal.ZERO) == 0){
            rate = newValue;
        }
        if(newValue.compareTo(BigDecimal.ZERO) != 0 && oldValue.compareTo(BigDecimal.ZERO) != 0){
            BigDecimal totalInc = newValue.subtract(oldValue);
            rate = totalInc.divide(oldValue,4,BigDecimal.ROUND_HALF_UP);
            rate = rate.multiply(new BigDecimal(100));
        }
        return rate;
    }
}

